package com.llkj.contractmanagement.contractmanagement.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.llkj.contractmanagement.R;
import com.llkj.contractmanagement.customview.RoundImageView;
import com.llkj.contractmanagement.model.ActorBean;
import com.llkj.contractmanagement.model.ContractBean;
import com.llkj.contractmanagement.utils.ImageLoaderUtil;

import java.util.List;

/**
 * author: dong.yj
 * created on: 2016/8/16 18:31
 * description:
 */
public class ActorAdapter extends BaseAdapter implements SectionIndexer {

    private List<ActorBean.YrlistBean> list = null;
    private Context mContext;

    public ActorAdapter(Context mContext, List<ActorBean.YrlistBean> list) {
        this.list = list;
        this.mContext = mContext;
    }

    /**
     * 当ListView数据发生变化时,调用此方法来更新ListView
     *
     * @param list
     */
    public void updateListView(List<ActorBean.YrlistBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return this.list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        final ActorBean.YrlistBean yrlistBean = list.get(position);
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.lv_actor_item, null);
            viewHolder.tv_actor_name = (TextView) convertView.findViewById(R.id.tv_actor_name);
            viewHolder.tv_actor_part = (TextView) convertView.findViewById(R.id.tv_actor_part);
            viewHolder.tvLetter = (TextView) convertView.findViewById(R.id.tv_lv_item_tag);
            viewHolder.riv_head_pic = (RoundImageView) convertView.findViewById(R.id.riv_head_actor);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        // 根据position获取分类的首字母的Char ascii值
        int section = getSectionForPosition(position);
        // 如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
        if (position == getPositionForSection(section)) {
            viewHolder.tvLetter.setVisibility(View.VISIBLE);
            viewHolder.tvLetter.setText(yrlistBean.getSortLetters());
        } else {
            viewHolder.tvLetter.setVisibility(View.GONE);
        }
        viewHolder.tv_actor_name.setText(yrlistBean.getName());
        viewHolder.tv_actor_part.setText(yrlistBean.getPart());
        ImageLoaderUtil.displayHeaderImage(yrlistBean.getLogo(),viewHolder.riv_head_pic);
        return convertView;

    }

    final static class ViewHolder {
        TextView tvLetter;//字母
        TextView tv_actor_name;//姓名
        TextView tv_actor_part;//姓名
        RoundImageView riv_head_pic;
    }


    /**
     * 根据ListView的当前位置获取分类的首字母的Char ascii值
     */
    public int getSectionForPosition(int position) {
        return list.get(position).getSortLetters().charAt(0);
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = list.get(i).getSortLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }

        return -1;
    }

    /**
     * 提取英文的首字母，非英文字母用#代替。
     *
     * @param str
     * @return
     */
    private String getAlpha(String str) {
        String sortStr = str.trim().substring(0, 1).toUpperCase();
        // 正则表达式，判断首字母是否是英文字母
        if (sortStr.matches("[A-Z]")) {
            return sortStr;
        } else {
            return "#";
        }
    }

    @Override
    public Object[] getSections() {
        return null;
    }
}
